// pages/register/register.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countries: ["外联部", "信息部", "人力资源部", "权益中心", "学术部", "志管部", "新媒体中心", "文体部", ""],
    countryIndex: 0,
    orgins: ["学生会", "团委"],
    orginIndex: 0,
    showTopTips: false,
    TopTip: '错误',
    name: '',
    teamPre: [],
    teamAfter: [],
    teamNum: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    app.login().then(function() {
      that.setData({
        teamPre: app.globalData.teamPre,
          data: app.globalData.data,
      })
      var t = [];
      for (let x in that.data.teamPre) {
        t.push(that.data.teamPre[x][1].split(' '))
      }
      that.setData({
        teamAfter: t
      })
    })
  },
  close: function () {
    wx.switchTab({
      url: '../newteam/newteam',
    })
  },

  showTopTips: function(str) {
    var that = this;
    this.setData({
      showTopTips: true,
      TopTip: str
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },
  submit: function() {
    if (!this.data.name) {
      this.showTopTips('请输入姓名！')
      return
    }
    if (this.data.teamNum >= 0) {
      let that = this
      console.log(this.data.countryIndex, this.data.orginIndex, this.data.name)

      that.setData({
        doing: true
      })
      let cookie = wx.getStorageSync('cookieKey');
      let header = {};
      if (cookie) {
        header.Cookie = cookie;
      }
      wx.request({
        url: app.globalData.url + '/wxapp/register',
        header: header,
        data: {
          teamNum: that.data.teamNum + 1,
          name: that.data.name,
          orgin: that.data.orgins[that.data.orginIndex],
          department: that.data.countries[that.data.countryIndex]
        },
        success: function(res) {
          console.log(res)
          that.setData({
            doing: false
          })
          wx.showToast({
            title: "注册成功！",
            duration: 3000
          })
          wx.navigateTo({
            url: '../newteam/newteam',
          })
        },
        fail:function(res){
          that.setData({
            doing: false
          })
        }
      })

    } else {
      this.showTopTips('您不在团建报名名单中！')
    }
  },
  bindKeyInput: function(e) {
    this.setData({
      name: e.detail.value
    })
    for (let i in this.data.teamAfter) {
      for (let j in this.data.teamAfter[i]) {
        console.log(i, j, this.data.teamAfter[i][j], e.detail.value)
        if (this.data.teamAfter[i][j] == e.detail.value) {
          this.setData({
            teamNum: parseInt(i)
          })
          return
        }
      }
    }
    this.setData({
      teamNum: -1
    })

  },
  bindOrginChange: function(e) {
    console.log('picker orgin 发生选择改变，携带值为', e.detail.value);
    this.setData({
      orginIndex: e.detail.value
    })
    if (e.detail.value == 0) {
      this.setData({
        countries: ["外联部", "信息部", "人力资源部", "权益中心", "学术部", "志管部", "新媒体中心", "文体部"]
      })
    } else {
      this.setData({
        countries: ["组织部", "宣传部", "实践部", "办公室"]
      })
    }
  },
  bindCountryChange: function(e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);

    this.setData({
      countryIndex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})